//类型判断并加载
window.addEventListener("DOMContentLoaded", function () {
   //
   //登入类型
   let type = 0; // 0-> 系统管理员  1-> 宿舍管理员  2-> 学生
   //登入账户 or 用户名
   let username = 'admin';
   //
   type = getCookie('type').split(',')[0];
   username = getCookie('type').split(',')[1];
   //获取 登录类型 用户名
   function getCookie(name) {
      var i = document.cookie.indexOf(name);
      if (i != -1) {
         var start = i + name.length + 1;
         var end = document.cookie.indexOf(";", i);
         if (end == -1) {
            end = document.cookie.length;
         }
         //console.log(document.cookie.substring(start,end));

         return document.cookie.substring(start, end);
      }
   }
   // console.log(type);

   //根据type登入类型加载相应数据
   if (type == 0) {
      //首页欢迎语 -- 系统管理员
      document.querySelector("#welcome_h1").innerHTML = "欢迎您，系统管理员！";
      //登录头像
      document.querySelector('.img-circle').src = './img/systemAdministrator.jpg';
      //登录用户名
      document.querySelector('#login_account').innerText = username;
      //欢迎语
      document.querySelector('#login_type').innerHTML = '&nbsp;&nbsp;&nbsp;欢迎您&nbsp;,&nbsp;&nbsp;系统管理员';
      //导航加载
      systemManagement.forEach(element => {
         if (element.title == '首页') {
            //首页 默认选中 active
            document.querySelector('#menus_div').innerHTML += `
                <a href="javascript:void(0);" class="list-group-item active" data-href="${element.href}">${element.title}
                   <span class="glyphicon glyphicon-menu-right menuRight" aria-hidden="true"></span>
                </a>
                `;
         }
         else {
            document.querySelector('#menus_div').innerHTML += `
                <a href="javascript:void(0);" class="list-group-item" data-href="${element.href}">${element.title}
                   <span class="glyphicon glyphicon-menu-right menuRight" aria-hidden="true"></span>
                </a>
                `;
         }
      });

   } else if (type == 1) {
      //首页欢迎语 -- 宿舍管理员
      document.querySelector("#welcome_h1").textContent = "欢迎您，宿舍管理员！";
      //登录头像
      document.querySelector('.img-circle').src = './img/houseparent.jpg';
      //登录用户名
      document.querySelector('#login_account').innerText = username;
      //欢迎语
      document.querySelector('#login_type').innerHTML = '&nbsp;&nbsp;&nbsp;欢迎您&nbsp;,&nbsp;&nbsp;宿舍管理员';
      //导航加载
      dormitoryManagement.forEach(element => {
         if (element.title == '首页') {
            document.querySelector('#menus_div').innerHTML += `
               <a href="javascript:void(0);" class="list-group-item active" data-href="${element.href}">${element.title}
                  <span class="glyphicon glyphicon-menu-right menuRight" aria-hidden="true"></span>
               </a>
               `;
         } else {
            document.querySelector('#menus_div').innerHTML += `
               <a href="javascript:void(0);" class="list-group-item" data-href="${element.href}">${element.title}
                  <span class="glyphicon glyphicon-menu-right menuRight" aria-hidden="true"></span>
               </a>
               `;
         }
      });

   } else if (type == 2) {
      //首页欢迎语 -- 学生
      document.querySelector("#welcome_h1").textContent = "欢迎您，同学！";
      //登录头像
      document.querySelector('.img-circle').src = './img/student.jpg';
      //登录用户名
      document.querySelector('#login_account').innerText = username;
      //欢迎语
      document.querySelector('#login_type').innerHTML = '&nbsp;&nbsp;&nbsp;欢迎您&nbsp;,&nbsp;&nbsp;同学';
      //导航加载
      studentManagement.forEach(element => {
         if (element.title == '首页') {
            document.querySelector('#menus_div').innerHTML += `
               <a href="javascript:void(0);" class="list-group-item active" data-href="${element.href}">${element.title}
                  <span class="glyphicon glyphicon-menu-right menuRight" aria-hidden="true"></span>
               </a>
               `;
         } else {
            document.querySelector('#menus_div').innerHTML += `
               <a href="javascript:void(0);" class="list-group-item" data-href="${element.href}">${element.title}
                  <span class="glyphicon glyphicon-menu-right menuRight" aria-hidden="true"></span>
               </a>
               `;
         }
      });

   } else {
      document.querySelector("#welcome_h1").textContent = "非法登录";
   }

   //导航选中效果
   let menus_div = document.querySelectorAll('#menus_div >a');
   menus_div.forEach(function (menu) {
      menu.setAttribute('class', 'list-group-item');
      if (menu.dataset.href == location.pathname.substring(1)) {
         menu.classList.add("active");
      }
   });
});



//数据
//系统管理员
let systemManagement = [
   { title: "首页", href: "welcome.html" },
   { title: "宿舍管理员管理", href: "dormAdminManager.html" },
   { title: "学生管理", href: "stuManagement.html" },
   { title: "宿舍楼管理", href: "dormManagement.html" },
   { title: "缺勤记录", href: "absence.html" },
   { title: "修改密码", href: "modifyPassword.html" },
]
//宿舍管理员
let dormitoryManagement = [
   { title: "首页", href: "welcome.html" },
   { title: "学生管理", href: "stuManagement.html" },
   { title: "缺勤记录", href: "absence.html" },
   { title: "修改密码", href: "modifyPassword.html" }
]
//学生管理
let studentManagement = [
   { title: "首页", href: "welcome.html" },
   { title: "缺勤记录", href: "absence.html" },
   { title: "修改密码", href: "modifyPassword.html" }
]

